<?php


namespace app\api\model;


use think\Model;

class Schedule extends  Model
{
    public static function scheduleStudentList($userid){
        $schedule = new Schedule();
        $User = new User();
        //查询学生所在的班级
        $class = $User->alias('u')->where('u.user_id',$userid)
            ->join('stu_user_relation ur','u.user_id = ur.user_id','left')//关系表
            ->field('ur.*')
            ->find();
        //查询课程表
        $resutl = $schedule->alias('s')->where('s.class_id',$class['class_id'])
            ->join('stu_class cs','cs.class_id = s.class_id','left')//班级表
            ->join('stu_major m','m.id = cs.major_id','left')//专业表
            ->join('stu_course co','co.course_id = s.course_id','left')//课程信息表
            ->join('stu_user u','u.user_id = co.user_id','left')//教师信息
            ->join('stu_sign_in si','si.schedule_id = s.schedule_id and si.user_id = u.user_id','left')//签到表
            ->field('s.*,cs.class_name,m.major_name,co.course_name,u.username,si.status')
            ->select();
        return $resutl;
    }

    public static function scheduleTeacherList($userid){
        $schedule = new Schedule();
        $resutl = $schedule->alias('s')->where('co.user_id',$userid)
            ->join('stu_class cs','cs.class_id = s.class_id','left')//班级表
            ->join('stu_major m','m.id = cs.major_id','left')//专业表
            ->join('stu_course co','co.course_id = s.course_id','left')//课程信息表
            ->join('stu_user u','u.user_id = co.user_id','left')//教师信息
            ->field('s.*,cs.class_name,m.major_name,co.course_name,u.username')
            ->select();
        return $resutl;
    }

    public static function StudentList($scheduleId){
        $user = new User();
        $schedule = new Schedule();
        $resutl1 = $schedule->where('schedule_id',$scheduleId)->find();//课程表
        $result = $user->alias('u')->where('ur.class_id',$resutl1['class_id'])->where('u.group_id',10001)//所有在该班级的学生
            ->join('stu_user_relation ur','u.user_id = ur.user_id','left')//关系表
            ->join('stu_sign_in si','si.schedule_id = '.$scheduleId.' and si.user_id = u.user_id','left')//签到表
            ->field('u.*,si.status')
            ->select();
        return $result;
    }

}